package com.ciban.smarthome.module.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.ImgUtils;
import com.ciban.smarthome.common.utils.RegexUtils;
import com.ciban.smarthome.common.utils.StringUtils;
import com.ciban.smarthome.module.engine.CurtainEngine;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.db.dao.EquipmentDao;
import com.ciban.smarthome.module.entity.Equip_Curtain;
import com.ciban.smarthome.module.net.protocol.Protocol;

public class E_CurtainModifyActivity extends BaseActivity implements
        OnClickListener {
    private static final int INTENT_CHOOSE_ICON = 100;

    private ImageView iv_icon;
    private Button bt_cancle, bt_save;
    private EditText et_no, et_name;

    private String icon_path = "imgs/light_icon_bd.png";

    private Equip_Curtain curtain;
    private boolean isEdit = false;

    private CurtainEngine engine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equip_curtain_modify);

        initData();

        initViews();
    }

    private void initData() {
        if (getIntent().hasExtra("curtain")) {
            isEdit = true;
            curtain = (Equip_Curtain) getIntent().getExtras().get("curtain");
        } else if (getIntent().hasExtra("type")) {
            curtain = new Equip_Curtain();
            curtain.setType(getIntent().getIntExtra("type", -1));
        }

        engine = new CurtainEngine(this);
    }

    private void initViews() {
        iv_icon = (ImageView) findViewById(R.id.curtain_add_icon);
        bt_cancle = (Button) findViewById(R.id.curtain_add_cancle);
        bt_save = (Button) findViewById(R.id.curtain_add_save);
        et_name = (EditText) findViewById(R.id.curtain_add_name);
        et_no = (EditText) findViewById(R.id.curtain_add_no);

        if (isEdit) {
            icon_path = curtain.getIcon_path();
            Bitmap bmp = ImgUtils.getImageFromAssetsFile(this, icon_path);
            iv_icon.setImageBitmap(bmp);

            et_name.setText(curtain.getName());
            et_no.setText(curtain.getSerial_no()+"");
        }

        setListeners();
    }

    private void setListeners() {
        iv_icon.setOnClickListener(this);
        bt_cancle.setOnClickListener(this);
        bt_save.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.curtain_add_icon:
                Intent intent = new Intent(E_CurtainModifyActivity.this,
                        IconChooseActivity.class);
                intent.putExtra("ids",
                        getResources().getStringArray(R.array.light_icons));
                startActivityForResult(intent, INTENT_CHOOSE_ICON);
                break;
            case R.id.curtain_add_cancle:
                finish();
                break;
            case R.id.curtain_add_save:
                addCurtain();
                break;
        }
    }

    private void addCurtain() {
        String no = et_no.getText().toString().trim();
        String name = et_name.getText().toString().trim();

        if (StringUtils.isEmpty(no) || !RegexUtils.isNumStr(no)) {
            displayShort("编号填写不正确");
            return;
        }

        if (StringUtils.isEmpty(name)) {
            displayShort("名称不能为空");
            return;
        }

        if (curtain == null)
            curtain = new Equip_Curtain();

        curtain.setName(name);
        curtain.setSerial_no(Integer.parseInt(no));
        curtain.setStatus(Protocol.COMMON_CLOSE);
        curtain.setIcon_path(icon_path + "");
        curtain.setArea_id(baseApp.getNowArea().getId());


        EquipmentDao dao = new EquipmentDao(this);
        boolean result = false;

        if (isEdit) {
            if (engine.updateCurtain(curtain))
                result = true;
        } else if (engine.addNewCurtain(curtain))
            result = true;


        if (result) {
            Intent data = new Intent();
            data.putExtra("curtain", curtain);
            setResult(RESULT_OK, data);
            displayShort("操作成功");
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case INTENT_CHOOSE_ICON:
                if (resultCode == RESULT_OK) {
                    icon_path = data.getStringExtra("path");
                    iv_icon.setImageBitmap(ImgUtils.getImageFromAssetsFile(this,
                            icon_path));
                }
                break;
        }
    }
}
